package com.ruoyi.biz.service.caseinfo.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.biz.bean.bo.CaseInfoBO;
import com.ruoyi.biz.bean.vo.dto.CaseInfoDTO;
import com.ruoyi.biz.bean.vo.qo.CaseInfoQO;
import com.ruoyi.biz.domain.CaseInfoCust;
import com.ruoyi.biz.domain.CaseInfoPhaseItem;
import com.ruoyi.biz.domain.CaseInfo;
import com.ruoyi.biz.domain.CaseType;
import com.ruoyi.biz.domain.LitigateType;
import com.ruoyi.biz.mapper.CaseInfoMapper;
import com.ruoyi.biz.service.caseinfo.ICaseInfoCustService;
import com.ruoyi.biz.service.caseinfo.ICaseInfoService;
import com.ruoyi.biz.service.caseinfoitem.ICaseInfoPhaseItemService;
import com.ruoyi.biz.service.casepahse.ICasePhaseItemService;
import com.ruoyi.biz.service.casetype.ICaseTypeService;
import com.ruoyi.biz.service.casetype.ILitigateTypeService;
import com.ruoyi.comm.crud.service.impl.BaseBQImpls;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.page.PageQO;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanCopier;

/**
 * 案件信息 服务层处理
 * 
 * @author riverbo
 */
@Service
public class CaseInfoServiceImpl extends BaseBQImpls<CaseInfoMapper, CaseInfo, CaseInfoBO, CaseInfoQO> implements ICaseInfoService {

	@Autowired
	private ICaseTypeService caseTypeService;

	@Autowired
	private ILitigateTypeService litigateTypeService;

	@Autowired
	private ICaseInfoCustService caseCustService;
	
	@Autowired
	private ICasePhaseItemService casePhaseItemService;
	
	@Autowired
	private ICaseInfoPhaseItemService caseInfoPhaseItemService;

	@Override
	public Boolean checkNameUnique(CaseInfo t) {
        Long boId = StringUtils.isNull(t.getCaseTypeId()) ? -1L : t.getCaseTypeId();
        Long dbId = this.baseMapper.getIdByName(t);
        if (StringUtils.isNotNull(dbId) && dbId.longValue() != boId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
	}
	
	@Override
	public List<CaseInfoBO> queryBO(CaseInfoQO q, PageQO page) {
		List<CaseInfoBO> list = super.queryBO(q, page);
		Map<Integer, CaseType> mapCaseType = caseTypeService.mapAll();
		Map<Integer, LitigateType> mapLt = litigateTypeService.mapAll();
		for (CaseInfoBO u : list) {
			Integer ctId = u.getCaseTypeId();
			if (ctId != null) {
				CaseType bean = mapCaseType.get(ctId);
				if (bean != null) {
					u.setCaseTypeName(bean.getCaseTypeName());
				}
			}
			//
			Integer ltId = u.getLitigateTypeId();
			if (ltId != null) {
				LitigateType bean = mapLt.get(ltId);
				if (bean != null) {
					u.setLitigateTypeName(bean.getLitigateTypeName());
				}
			}
			Map<Integer, List<CaseInfoCust>> caseCustMap = caseCustService.getMap(u.getCaseId());
			if (!MapUtils.isEmpty(caseCustMap)) {
				//StringBuilder sb = new StringBuilder();
			    List<CaseInfoCust> principal = caseCustMap.get(1);
			    u.setPrincipal(principal);
			    //getTitle(principal, sb);
			    
			    List<CaseInfoCust> oppositor = caseCustMap.get(2);
			    u.setOppositor(oppositor);
			    //getTitle(oppositor, sb);
			    
			    List<CaseInfoCust> thirdParty = caseCustMap.get(3);
			    u.setThirdParty(thirdParty);
			    //getTitle(thirdParty, sb);
			    //
			    //u.setCaseName(sb.toString());
			}
		}
		return list;
	}
	
//	private void getTitle(List<CaseCust> list, StringBuilder sb) {
//		//StringBuilder sb = new StringBuilder();
//		if (!CollectionUtils.isEmpty(list)) {
//			for (CaseCust u : list) {
//				if (sb.length()>0) {
//					sb.append("; ");
//				}
//				sb.append(u.getCustName());
//			}
//		}
//		//return sb.toString();
//	}
	
	public CaseInfoBO selectBOById(Long id) {
		CaseInfoBO u = super.selectBOById(id);
		Map<Integer, CaseType> mapCaseType = caseTypeService.mapAll();
		Map<Integer, LitigateType> mapLt = litigateTypeService.mapAll();
		Integer ctId = u.getCaseTypeId();
		if (ctId != null) {
			CaseType bean = mapCaseType.get(ctId);
			if (bean != null) {
				u.setCaseTypeName(bean.getCaseTypeName());
			}
		}
		//
		Integer ltId = u.getLitigateTypeId();
		if (ltId != null) {
			LitigateType bean = mapLt.get(ltId);
			if (bean != null) {
				u.setLitigateTypeName(bean.getLitigateTypeName());
			}
		}
		Map<Integer, List<CaseInfoCust>> caseCustMap = caseCustService.getMap(u.getCaseId());
		if (!MapUtils.isEmpty(caseCustMap)) {
			//StringBuilder sb = new StringBuilder();
		    List<CaseInfoCust> principal = getListOfMap(caseCustMap, 1);
		    u.setPrincipal(principal);
		    //getTitle(principal, sb);
		    
		    List<CaseInfoCust> oppositor = getListOfMap(caseCustMap, 2);
		    u.setOppositor(oppositor);
		    //getTitle(oppositor, sb);
		    
		    List<CaseInfoCust> thirdParty = getListOfMap(caseCustMap, 3);
		    u.setThirdParty(thirdParty);
		    //getTitle(thirdParty, sb);
		    //
		    //u.setCaseName(sb.toString());
		}
		return u;
	}
	
	private List<CaseInfoCust> getListOfMap(Map<Integer, List<CaseInfoCust>> map, int n) {
		List<CaseInfoCust> list = map.get(n);
		if (!CollectionUtils.isEmpty(list)) {
			return list;
		} else {
			return new ArrayList<>(1);
		}
	}
	
	@Override
	public int insertA(Object a) {
		CaseInfoDTO dto = (CaseInfoDTO)a;
		CaseInfo bean = BeanCopier.copyProperties(a, this.getEntityClass());
		int n = super.insert(bean);
		int id = bean.getCaseId();
		n += this.caseCustService.insert(id, dto.getPrincipal());
		n += this.caseCustService.insert(id, dto.getOppositor());
		n += this.caseCustService.insert(id, dto.getThirdParty());
		return n;
	}
	
	@Override
	public int deleteByIds(Long[] ids) {
		int n = super.deleteByIds(ids);
		if ( n > 0 ) {
			n += this.caseCustService.deleteByIds(ids);
		}
		return n;
	}
	
	@Override
	public <A> int updateA(A a) {
		CaseInfoDTO dto = (CaseInfoDTO)a;
		CaseInfo bean = BeanCopier.copyProperties(a, this.getEntityClass());
		int n = super.update(bean);
		int id = bean.getCaseId();
		n += this.caseCustService.deleteById(id);
		n += this.caseCustService.insert(id, dto.getPrincipal());
		n += this.caseCustService.insert(id, dto.getOppositor());
		n += this.caseCustService.insert(id, dto.getThirdParty());
		return n;
	}

    @Override
    public <A> int checkA(A a) {
    	CaseInfoDTO dto = (CaseInfoDTO)a;
    	CaseInfo store = super.selectById(dto.getCaseId());
    	if (store == null) {
    		return 0;
    	}
    	if (!StringUtils.equals(store.getStatus(), "0")) {
    		return 0;
    	}
    	if (!StringUtils.equals(store.getCheckStatus(), "0")) {
    		return 0;
    	}
    	dto.setStatus("1");
    	dto.setCheckStatus("1");
    	if (StringUtils.isEmpty(store.getCaseNo())) {
    		String day = DateUtils.dateTime();
    		String caseNo = String.format("%s%02d%05d", day, store.getCaseTypeId(), store.getCaseId());
    		dto.setCaseNo(caseNo);
    	}
    	int n = super.checkA(dto);
		if (n > 0) {
			n += this.caseInfoPhaseItemService.deleteById(dto.getCaseId());
			// 获取当前案件阶段 (cur) 的 phaseItems
			List<CaseInfoPhaseItem> lsCaseInfoPhaseItem = casePhaseItemService.selectListById(dto.getCasePhaseId(), CaseInfoPhaseItem.class);
			n += this.caseInfoPhaseItemService.insert(dto.getCaseId(), lsCaseInfoPhaseItem);
		}
    	return n;
    }

}
